﻿var report = {
    ReferralCreated: true,
    GetData: function (fromDate, toDate, listMDId, clinic, groupBy, isCreate) {
        common.waiting();
        var data = { fromDate: fromDate, toDate: toDate, listMDId: JSON.stringify(listMDId), clinic: clinic, groupBy: groupBy, isCreate: isCreate };

        jQuery.getJSON(common.GetUrl() + "Report/GetData?" + Math.random(), data, function (result) {
            $('.refer_report_right').html(result.content);
            $('.excludeReferral').html(result.excludeReferral);
            //fill hidden value
            $("#hdfFromDate").val(fromDate);
            $("#hdfToDate").val(toDate);
            $("#hdfListMDId").val(JSON.stringify(listMDId));
            $("#hdfClinic").val(clinic);
            $("#hdfGroupBy").val(groupBy);
            $("#hdfIsCreate").val(isCreate);
        });
    },
    GetReport: function () {
        //        if (validAllDate() == false)
        //            return;
        var sliderDiv = $("#validDate");
        if (sliderDiv.find('.field-validation-error').length > 0)
            return;
        var fromDateStr = $("#txtDateFrom").val().split('/');
        var toDateStr = $("#txtDateTo").val().split('/');
        var fromDate = fromDateStr[0] + "/01/" + fromDateStr[1];
        var toDate = toDateStr[0] + "/01/" + toDateStr[1];
        listMDId = [];
        var currrentMD = $("#MDNPInfo_AssignmentRelationship").val();
        if (currrentMD > 0) {
            var profile = new Object();
            profile.ProfileId = currrentMD;
            listMDId.push(profile);
        }
        else {
            $("#MDNPInfo_AssignmentRelationship option").each(function () {
                var profile = new Object();
                profile.ProfileId = $(this).val();
                listMDId.push(profile);
            });
        }
        var group = $("#txtGroup").val() != "Select grouping..." ? $("#ModelSelectGrouping_Id").val() : "";
        var location = $("#Organization_OrganizationId").val();
        report.GetData(fromDate, toDate, listMDId, location, group, report.ReferralCreated);
    },
    searchKeyup: function (e) {
        if (e.keyCode == 13)
            report.searchDatatable();
    },
    searchDatatable: function () {
        var keyword = $.trim($("#txtSearch").val().toLowerCase());
        $("#contentDataTable .content").each(function () {
            if ($(this).find(".contentLeft").text().toLowerCase().indexOf(keyword) != -1)
                $(this).show();
            else
                $(this).hide();
        });
    },
    SendRelationship: function () {
        var Objs = [];
        $("#contentExcludingFrom p").each(function () {
            var obj = new Object();
            obj.ProfileId = $(this).find("#hdfProfileId").val();
            obj.OrganizationId = $(this).find("#hdfOrganizationId").val();
            obj.RequestToEmail = $(this).find("#hdfProfileEmail").val();
            Objs.push(obj);
        });        
        jQuery.post(common.GetUrl() + "Report/SendRequest", { referralStr: JSON.stringify(Objs), referralCreated: report.ReferralCreated }, function (result) {
            if (Objs.length > 1)
                alert("Requests have been sent.");
            else
                alert("Request has been sent.");
            $("#btnSend").removeAttr("onclick");
            $("#btnSend").css("color", "#999999");
            $("#btnSend").css("cursor", "default");
        });
    }
}